import { createRouter, createWebHistory } from 'vue-router';
import { getAccessToken } from 'axiom-view'; // 从 axiom-view 导入 token 工具
import FullPageLayout from '@/layout/FullPageLayout.vue';
import { AxiomPCMainLayout, AxiomRouteView } from 'axiom-view';
import HomeView from '@/views/HomeView.vue';

// [重构] 这是 demo 应用自己的路由表
const routes = [
    // ... (您现有的路由表)
    // 此处为了简洁，省略具体路由，实际应为 RoleManagement.vue 中的完整路由
    {
    path: '/login',
    component: FullPageLayout,
    children: [
      {
        path: '',
        name: 'Login',
        component: () => import('@/views/login/index.vue'),
        meta: { title: '登录' },
      }
    ]
  },
  {
    path: '/:pathMatch(.*)*',
    component: FullPageLayout,
    children: [
        {
            path: '',
            name: 'NotFound',
            component: () => import('@/views/error/404.vue'),
            meta: { title: '404 - 页面未找到' },
        }
    ]
  },
  {
    path: '/403',
    component: FullPageLayout,
    children: [
        {
            path: '',
            name: 'Forbidden',
            component: () => import('@/views/error/403.vue'),
            meta: { title: '403 - 无权限访问' },
        }
    ]
  },
  {
    path: '/500',
    component: FullPageLayout,
    children: [
        {
            path: '',
            name: 'ServerError',
            component: () => import('@/views/error/500.vue'),
            meta: { title: '500 - 服务器错误' },
        }
    ]
  },
  {
    path: '/',
    component: AxiomPCMainLayout,
    children: [
      {
        path: '',
        name: 'Home',
        component: HomeView,
        meta: { title: '首页', icon: 'home', hasSidebar: false }
      },
      {
        path: 'demo',
        name: 'Demo',
        component: AxiomRouteView,
        redirect: '/demo/pc-components',
        meta: { title: '组件示例', icon: 'apps' },
        children: [
          {
            path: 'pc-components',
            name: 'PCComponents',
            component: () => import('@/views/demo/PCComponentsView.vue'),
            meta: { title: 'PC 专属组件' }
          },
          {
            path: 'component-showcase',
            name: 'ComponentShowcase',
            component: () => import('@/views/demo/ComponentShowcaseView.vue'),
            meta: { title: '通用组件' }
          },
        ]
      },
      {
        path: 'system',
        name: 'System',
        component: AxiomRouteView,
        redirect: '/system/user',
        meta: { title: '系统管理', icon: 'setting' },
        children: [
          {
            path: 'user',
            name: 'UserManagement',
            component: () => import('@/views/system/user/UserManagement.vue'),
            meta: { title: '用户管理', icon: 'user' },
          },
          {
            path: 'role',
            name: 'RoleManagement',
            component: () => import('@/views/system/role/RoleManagement.vue'),
            meta: { title: '角色管理', icon: 'role' },
          },
          {
            path: 'permission',
            name: 'PermissionManagement',
            component: () => import('@/views/system/permission/PermissionManagement.vue'),
            meta: { title: '权限管理', icon: 'permission' }
          },
          {
            path: 'client',
            name: 'ClientManagement',
            component: () => import('@/views/system/client/ClientManagement.vue'),
            meta: { title: '客户端管理', icon: 'laptop' }
          },
        ]
      },
      {
        path: 'knowledge',
        name: 'Knowledge',
        component: AxiomRouteView,
        redirect: '/knowledge/knowledge-base',
        meta: { title: '知识管理', icon: 'database' },
        children: [
          {
            path: 'knowledge-base',
            name: 'KnowledgeBases',
            component: () => import('@/views/knowledge/knowledge-base/index.vue'),
            meta: { title: '知识库管理', icon: 'database' },
          },
          {
            path: 'query',
            name: 'KnowledgeQuery',
            component: () => import('@/views/knowledge/query/index.vue'),
            meta: { title: '知识库查询', icon: 'search' },
          },
          {
            path: '/knowledge/:kb_id/documents',
            name: 'KnowledgeBaseDocuments',
            component: () => import('@/views/knowledge/document/index.vue'),
            props: route => ({ knowledgeBaseId: route.params.kb_id })
          },
          {
            path: 'indexing/:kb_id',
            name: 'KnowledgeBaseIndexing',
            component: () => import('@/views/knowledge/indexing/index.vue'),
            props: route => ({ 
              knowledgeBaseId: route.params.kb_id,
              // Document IDs will now be passed via history.state
            })
          },
          {
            path: 'file',
            name: 'KnowledgeFile',
            component: () => import('@/views/knowledge/file/index.vue'),
            meta: { title: '文件库管理', icon: 'file' },
          },
        ]
      },
      {
        path: 'ai',
        name: 'AI',
        component: AxiomRouteView,
        redirect: '/ai/chat',
        meta: { title: 'AI 服务', icon: 'MagicStick' },
        children: [
          {
            path: 'chat',
            name: 'AIChat',
            component: () => import('@/views/ai-chat/index.vue'),
            meta: { title: '知识库AI查询' },
          },
        ],
      },
    ]
  },
];

// [重构] demo 应用现在使用官方的 createRouter 来创建自己的路由实例
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes,
});

// [重构] demo 应用现在拥有自己的路由守卫
router.beforeEach((to, from, next) => {
  const hasToken = getAccessToken();
  const whitelist = ['/login']; // 登录页在白名单中

  if (hasToken) {
    next(); // 如果有 token，允许访问任何页面
  } else {
    // 如果没有 token
    if (whitelist.includes(to.path) || to.name === 'NotFound') {
      next(); // 允许访问白名单内的页面
    } else {
      // 其他所有页面都重定向到登录页
      next(`/login?redirect=${to.path}`);
    }
  }
});

export default router; 